package com.yulan.bankback.service.admin.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yulan.bankback.entity.event.AttackEvent;
import com.yulan.bankback.entity.model.admin.webAttack.IpBlackList;
import com.yulan.bankback.mapper.admin.webAttack.AttackEventMapper;
import com.yulan.bankback.mapper.admin.webAttack.IpBlackListMapper;
import com.yulan.bankback.service.admin.WebAttackService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class WebAttackServiceImpl implements WebAttackService {

    @Resource
    private AttackEventMapper attackEventMapper;

    @Resource
    private IpBlackListMapper ipBlackListMapper;

    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat formatter3 = new SimpleDateFormat("MM-dd");


    @Override
    public List<AttackEvent> getAllAttackEvent() {
        List<AttackEvent> res = attackEventMapper.selectList(
                new QueryWrapper<AttackEvent>().last("limit 500"));
        res.forEach(item -> item.setViewTime(formatter.format(item.getCreateTime())));
        return res;
    }

    @Override
    public List<AttackEvent> getAttackEventConditions(String start, String end, String type,
                                                      String level, String ip) {
        QueryWrapper<AttackEvent> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(start))
            queryWrapper.gt("create_time", DateUtil.parseDate(start));
        if(StringUtils.isNotBlank(end))
            queryWrapper.lt("create_time", DateUtil.parseDate(end));
        if(StringUtils.isNotBlank(type))
            queryWrapper.eq("type", type);
        if(StringUtils.isNotBlank(level))
            queryWrapper.eq("level", level);
        if(StringUtils.isNotBlank(ip))
            queryWrapper.eq("ip", ip);
        List<AttackEvent> res = attackEventMapper.selectList(queryWrapper);
        res.forEach(item -> item.setViewTime(formatter.format(item.getCreateTime())));
        return res;
    }

    @Override
    public List<IpBlackList> getAllIpBlackList() {
        List<IpBlackList> res = ipBlackListMapper.selectList(
                new QueryWrapper<IpBlackList>().last("limit 500"));
        res.forEach(item -> item.setViewTime(formatter.format(item.getCreateTime())));
        return res;
    }

    @Override
    public List<IpBlackList> getIpBlackListConditions(String start, String end, String type, String country, String city, String ip) {
        QueryWrapper<IpBlackList> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(start))
            queryWrapper.gt("create_time", DateUtil.parseDate(start));
        if(StringUtils.isNotBlank(end))
            queryWrapper.lt("create_time", DateUtil.parseDate(end));
        if(StringUtils.isNotBlank(type))
            queryWrapper.eq("attack_type", type);
        if(StringUtils.isNotBlank(country))
            queryWrapper.eq("country", country);
        if(StringUtils.isNotBlank(city))
            queryWrapper.eq("city", city);
        if(StringUtils.isNotBlank(ip))
            queryWrapper.eq("ip", ip);
        List<IpBlackList> res = ipBlackListMapper.selectList(queryWrapper);
        res.forEach(item -> item.setViewTime(formatter.format(item.getCreateTime())));
        return res;
    }


}
